package org.example.csa.controller;

import com.alipay.sofa.rpc.config.ConsumerConfig;
import org.example.csa.rest.AjaxResponse;
import org.example.inner.dto.req.AccReqDTO;
import org.example.inner.dto.resp.AccRespDTO;
import org.example.inner.inter.HelloService;
import org.example.inner.inter.acc.IAccountService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestApi {
    private static final ConsumerConfig<HelloService> helloConsumerConfig;
    private static final ConsumerConfig<IAccountService> accConsumerConfig;


    static {
        helloConsumerConfig = new ConsumerConfig<HelloService>()
                .setInterfaceId(HelloService.class.getName()) // 指定接口
                .setProtocol("bolt") // 指定协议
                .setDirectUrl("bolt://127.0.0.1:12200"); // 指定直连地址

        accConsumerConfig = new ConsumerConfig<IAccountService>()
                .setInterfaceId(IAccountService.class.getName()) // 指定接口
                .setProtocol("bolt") // 指定协议
                .setDirectUrl("bolt://127.0.0.1:12200"); // 指定直连地址
    }

    @GetMapping("/test01")
    @SuppressWarnings("unchecked")
    public AjaxResponse<String> test01() {

        // 生成代理类
        HelloService helloService = helloConsumerConfig.refer();

        return AjaxResponse.success(helloService.sayHello("qwe123"));
    }

    @GetMapping("/test02")
    public AjaxResponse<AccRespDTO> test02() {
        // 生成代理类
        IAccountService accountService = accConsumerConfig.refer();
        return AjaxResponse.success(accountService.getOneAccInfo(AccReqDTO.builder()
                        .email("4we65r4@126.com")
                        .name("qw541f")
                .build()));
    }
}
